package com.factory.test.m5622;

import android.os.Bundle;
import android.os.SystemClock;
import android.os.yx.YxDeviceManager;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.base.MyApplication;
import com.factory.test.databinding.ActivityM5622GpioBinding;
import com.yecontech.sdk.base.gpio.GpioCustom;
import com.yecontech.sdk.base.gpio.YTGpio;
import com.yecontech.sdk.base.gpio.YTGpioStatusListener;


/**
 * GPIO 输出, 摄像头电源测试
 */
public class GpioActivity extends BaseTestActivity {

    private ActivityM5622GpioBinding binding;
YxDeviceManager yx;
//    private GpioCustom io1;
//    private GpioCustom io2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_m5622_gpio);
yx = YxDeviceManager.getInstance(this);

        binding.typeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.typeInput){
                    yx.setGpioDirection(4,1);
                    yx.setGpioDirection(20,1);
                    binding.gpio1.setChecked(yx.getGpioValue(4)==1);
                    binding.gpio2.setChecked(yx.getGpioValue(20)==1);
                }else if(checkedId == R.id.typeOut){
                    yx.setGpioDirection(4,0);
                    yx.setGpioDirection(20,0);
                    binding.gpio1.setChecked(yx.getGpioValue(4)==1);
                    binding.gpio2.setChecked(yx.getGpioValue(20)==1);
                }
            }
        });
        yx.setGpioDirection(4,1);
        yx.setGpioDirection(20,1);
        binding.typeInput.setChecked(true); //默认为输入
        binding.gpio1.setChecked(yx.getGpioValue(4)==1);
        binding.gpio2.setChecked(yx.getGpioValue(20)==1);
        binding.gpio1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                yx.setGpioValue(4,b ? 1 : 0);
            }
        });
        binding.gpio2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                yx.setGpioValue(20,b ? 1 : 0);
            }
        });
    }

//    private void initType(int type){
//        if(io1 != null){
//            io1.release();
//            SystemClock.sleep(200);
//        }
//
//        io1 =  MyApplication.getBaseFactoryTest().getDeviceSdkService().getGpioCustomManager();
//        io1.init("4", type, new YTGpioStatusListener() {
//            @Override
//            public void onActive(YTGpio ytGpio, int activeType) {
//                handler.post(() -> binding.gpio1.setChecked(activeType == 1));
//            }
//        });
//        binding.gpio1.setChecked(io1.getAction() == 1);
//        binding.gpio1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                io1.setAction(b ? 1 : 0);
//            }
//        });
//
//
//        if(io2 != null){
//            io2.release();
//            SystemClock.sleep(200);
//        }
//        io2 =  MyApplication.getBaseFactoryTest().getDeviceSdkService().getGpioCustomManager();
//        io2.init("20", type, new YTGpioStatusListener() {
//            @Override
//            public void onActive(YTGpio ytGpio, int activeType) {
//                handler.post(() -> binding.gpio2.setChecked(activeType == 1));
//            }
//        });
//        binding.gpio2.setChecked(io2.getAction() == 1);
//        binding.gpio2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                io2.setAction(b ? 1 : 0);
//            }
//        });
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        io1.release();
//        io2.release();
    }
}
